using Nop.Core.Plugins;
using Nop.Plugin.Misc.MessageDesigner.Data;
using Nop.Plugin.Misc.MessageDesigner.Services;
using Nop.Services.Configuration;
using Nop.Services.Localization;
using Nop.Web.Framework.Menu;
using System.Linq;
using System.Web.Routing;

namespace Nop.Plugin.Misc.MessageDesigner
{
    /// <summary>
    /// PLugin
    /// </summary>
    public class MessageDesignerPlugin : BasePlugin,IAdminMenuPlugin
    {

        private readonly MessageDesignerObjectContext _objectContext;
        private readonly ISettingService _settingService;
        private readonly IMessageDesignerService _messageDesignerService;
        public MessageDesignerPlugin(MessageDesignerObjectContext objectContext, ISettingService settingService, 
            IMessageDesignerService messageDesignerService)
        {
            this._objectContext = objectContext;
            this._settingService = settingService;
            this._messageDesignerService = messageDesignerService;
        }
        
        /// <summary>
        /// Install plugin
        /// </summary>
        public override void Install()
        {
            //"settings.valid_children= '+body[style]';"
            var richEditorSettings = _settingService.GetSetting("adminareasettings.richeditoradditionalsettings");
            if (richEditorSettings == null)
                _settingService.SetSetting("adminareasettings.richeditoradditionalsettings", "settings.valid_children= '+body[style]';");
            else
            {
                if (!richEditorSettings.Value.Contains("settings.valid_children= '+body[style]'"))
                {
                    richEditorSettings.Value += "settings.valid_children= '+body[style]';";
                    _settingService.SetSetting("adminareasettings.richeditoradditionalsettings", richEditorSettings.Value);
                }
            }
            
            //data
            _objectContext.Install();
            _messageDesignerService.ExecuteSQL("Install.sql");
            //locals
            //controller
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Added", "Item Add");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Updated", "Item updated");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Deleted", "Item deleted");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Successfully", "The message design has been added to template successfully");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Removed", "The message design has been removed from template successfully");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Failed", "Error adding message design to template");
            //models
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.SearchStore", "Search store");
            this.AddOrUpdatePluginLocaleResource("plugins.misc.messagedesigner.SearchStore.Hint", "Search store");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name.Hint", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body", "Body");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body.Hint", "Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AdminComment", "Admin Comment");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AdminComment.Hint", "Admin Comment");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.LimitedToStores", "Limited to stores");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.LimitedToStores.Hint", "Limited to stores");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AvailableStores", "Available stores");
            this.AddOrUpdatePluginLocaleResource("plugins.misc.messagedesigner.fields.AvailableStores.Hint", "Available Stores");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Select", "Select");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.View", "View");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.DesignName", "Design Name");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Select", "Select");
            //validators
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name.Required", "Name required");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body.Required", "Body required");
            //views
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Info", "Info");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Stores", "Stores");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.CreateMessageDesignDetails", "Create Message Design Details");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.BackToList", "Back to list");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.EditMessageDesignDetails", "Edit Message Design Details");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner", "Message Designer");

            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.NopMessageTemplates", "Nop Message Templates");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.SaveBeforeEdit", "You need to save the design before you can add it to message templates.");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.ApplyToAll", "Apply to all");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.ApplyToSelected", "Apply to selected");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.Instructions", "<div><strong>Instructions</strong></div><ol><li>Create a message design in HTML.</li><li>Use Editor -> tools -> view source past the html code.</li><li>Place the following code in your html design</li><li>The original Nop template with the tokens will be replacing the code</li><li>The code should be placed on each language that has design.</li><li>DO NOT EDIT OR DELETE THAT DIV</li></ol>");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.InstructionsEdit", "<div><strong>Replacment Code</strong></div><ol><li>The following code is essential.</li><li>Do not delete or edit the code</li><li>I the code does not exist, copy and place in the design HTML code</li></ol>");           
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.RestoreNop", "Restore Nop Templates");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.RestoreNop.Confirm", "Are you sure you would like to RESTORE NOP TEMPLATES?");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.RemoveDesign", "Remove Design");
            this.AddOrUpdatePluginLocaleResource("Plugins.Misc.MessageDesigner.ManageDesigns", "Manage Designs");
            
            base.Install();
        }

        /// <summary>
        /// Uninstall plugin
        /// </summary>
        public override void Uninstall()
        {
            //remove designs
            _messageDesignerService.RemoveAllDesigns();
            //data
            _objectContext.Uninstall();

            //controller
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Added");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Updated");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Deleted");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Successfully");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Removed");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Failed");
            //models
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.SearchStore");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.SearchStore.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AdminComment");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AdminComment.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.LimitedToStores");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.LimitedToStores.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AvailableStores");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.AvailableStores.Hint");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Select");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.View");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.DesignName");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Select");
            //validators
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Name.Required");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Fields.Body.Required");
            //views
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Info");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Stores");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.CreateMessageDesignDetails");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.BackToList");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.EditMessageDesignDetails");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.NopMessageTemplates");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.SaveBeforeEdit");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.ApplyToAll");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.ApplyToSelected");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.Instructions");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.InstructionsEdit");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.RestoreNop");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.RestoreNop.Confirm");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.RemoveDesign");
            this.DeletePluginLocaleResource("Plugins.Misc.MessageDesigner.ManageDesigns");
            
            base.Uninstall();
        }

        public void ManageSiteMap(SiteMapNode rootNode)
        {
            var menuItem = new SiteMapNode()
            {
                SystemName = "Hz",
                Title = "Message Designer",
                        ControllerName = "MiscMessageDesigner",
                        ActionName = "List",
                        Visible = true,
                        RouteValues = new RouteValueDictionary() { { "area", null } },
            };

            var pluginNode = rootNode.ChildNodes.FirstOrDefault(x => x.SystemName == "Third party plugins");
            if (pluginNode != null)
                pluginNode.ChildNodes.Add(menuItem);
            else
                rootNode.ChildNodes.Add(menuItem);
        }

    }
}
